package com.top10.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.top10.bean.JwtUtil;
import com.top10.bean.PageBean;
import com.top10.bean.ResponseBean;
import com.top10.entity.Address;
import com.top10.entity.Shop;
import com.top10.entity.UserCollect;
import com.top10.entity.vo.ShopVo;
import com.top10.service.AddressService;
import com.top10.service.IShopService;
import com.top10.service.IUserCollectService;
import com.top10.utils.DistanceUtil;
import com.top10.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 店铺Controller
 *
 * @author ellen
 * @version 1.0
 * @date 2018/9/20 19:23
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    IShopService shopService;
    @Autowired
    AddressService addressService;
    /**
     * 店铺列表
     *
     * @param type
     * @param sort 默认-1不排序 0销量排序 1好评排序
     * @return
     */
    @RequestMapping("/list")
    public ResponseBean list(@RequestParam(value = "type", defaultValue = "") String type, @RequestParam(value = "sort", defaultValue = "-1") Integer sort) {
        List<ShopVo> shops = shopService.shopList(type);

        //销量排序
        if (sort.equals(0)) {
            Collections.sort(shops, new Comparator<ShopVo>() {
                @Override
                public int compare(ShopVo s1, ShopVo s2) {
                    if (s1.getSaleCount() == null) {
                        return 1;
                    }
                    if (s2.getSaleCount() == null) {
                        return -1;
                    }
                    if (s1.getSaleCount() > s2.getSaleCount()) {
                        return -1;
                    }
                    if (s1.getSaleCount() < s2.getSaleCount()) {
                        return 1;
                    }
                    return 0;
                }
            });
        }

        //好评排序
        if (sort.equals(1)) {
            Collections.sort(shops, new Comparator<ShopVo>() {
                @Override
                public int compare(ShopVo s1, ShopVo s2) {
                    if (s1.getGoodCount() > s2.getGoodCount()) {
                        return -1;
                    }
                    if (s1.getGoodCount() < s2.getGoodCount()) {
                        return 1;
                    }
                    return 0;
                }
            });
        }

        return ResponseBean.createSuccess().addData("shops", shops);
    }

    /**
     * 查询店铺详情
     *
     * @param
     * @return
     */
    @RequestMapping("/shopDetail")
    public ResponseBean shopDetail( String id) {
        if (id == null || id.trim().equals("")) {
            return ResponseBean.createError("店铺uuid不能为空");
        }
        Shop shop = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid", id));
        return ResponseBean.createSuccess().addData("shopDetail", shop);
    }

    /**
     * 店铺搜索
     *
     * @param keyword
     * @return
     */
    @RequestMapping("/searchShop")
    public ResponseBean searchShop(String keyword,PageBean pageBean) {
        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Shop> shops = shopService.selectList(new EntityWrapper<Shop>().like("name", "%" + keyword + "%").eq("isdeleted", false).eq("status",1));
        if(shops.size()==0){
            return ResponseBean.createSuccess("没有此类关键词的商店");
        }
        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        return ResponseBean.createSuccess().addData("shops", shops).addData("pageBean",pageBean);
    }
    /**
     * 获取客服号
     */
    @RequestMapping("/getseller")
    public ResponseBean getSeller(String province, String city, String district, String shop_uuid)throws Exception {
        Shop shop = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid", shop_uuid));
        if (null != shop && shop.getShopType() == 4) {
            String uuid = shopService.getAgentUUid(province, city, district, shop_uuid);
            Shop shop1 = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid", uuid));
            return ResponseBean.createSuccess().addData("shop",shop1);
        }else {
            return ResponseBean.createSuccess().addData("shop",shop);
        }







        /*    int temp = Integer.MAX_VALUE;
           List<Shop> shopList=shopService.selectList(new EntityWrapper<Shop>().eq("parent_uuid",shop_uuid).eq("status",1).like("sellerid", "de_%"));
            if (shopList.size() == 0) {
               String uuid = shop.getUuid();
                Shop shop2 = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid", uuid));
                return ResponseBean.createSuccess().addData("shop",shop2);
            }
         *//*   for (int i = 0; i < shopList.size(); i++) {
                String province1 = shopList.get(i).getProvince();
                String city1 = shopList.get(i).getCity();
                String district1 = shopList.get(i).getDistrict();
                String fullAddress = DistanceUtil.fullAddress(province1, city1, district1);

                Address agentAddress = addressService.selectByFullName(fullAddress);

                String agentLat = agentAddress.getLat();
                String agentIng = agentAddress.getIng();

                String distanceStr = DistanceUtil.getDistance(address.getLat(), address.getIng(), agentLat, agentIng);
                int distance = Integer.parseInt(distanceStr);
                // 保存距离最小的uuid
                if (distance < temp) {
                    temp = distance;
                    uuid1 = shopList.get(i).getUuid();
              *//**//*      System.out.println("当前距离最小代理商地址：" + fullAddress + "当前距离：" + temp);
                    System.out.println("当前代理商uuid：" + uuid);
                    System.out.println("当前的代理商店铺名称：" + agentShopList.get(i).getName());*//**//*
                }
            }*//*
            Shop shop1 = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid",uuid1));

            return ResponseBean.createSuccess().addData("shop",shop1);


*/


    }

}